using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.IO;

namespace DeVierKoeden.Dynamicweb.CreateSite.Models
{
  /// <summary>
  /// Wrapper object to show information about an IIS site that runs Dynamicweb.
  /// </summary>
  [ReadOnly(true)]
  public class DynamicwebSite
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="DynamicwebSite"/> class.
    /// </summary>
    public DynamicwebSite()
    {
      SqlSettings = new SqlServerSettings();
    }
    /// <summary>
    /// Gets or sets the ID of the site in IIS.
    /// </summary>
    public long Id { get; set; } // Needs to be public for data binding to work

    /// <summary>
    /// Gets or sets the name of the site in IIS.
    /// </summary>
    public string Name { get; set; } // Needs to be public for data binding to work

    /// <summary>
    /// Gets or sets a value that determines if this site is set up as a custom solution.
    /// </summary>
    [DisplayName("Is Custom Solution")]
    [Description("Whether the site has its own .NET Project at the root.")]
    public bool IsCustom { get; set; }

    /// <summary>
    /// Gets or sets the root path of the site. This is the Application folder for a custom project or the application folder under Applications for a standard setup.
    /// </summary>
    [Category("Paths")]
    [DisplayName("Path to root of the site")]
    public string RootPath { get; set; }

    /// <summary>
    /// Gets or sets the physical path to the /Admin folder.
    /// </summary>
    [Category("Paths")]
    [DisplayName("Path to admin folder")]
    public string AdminPath { get; set; }

    /// <summary>
    /// Gets or sets the physical path to the /Files folder.
    /// </summary>
    [Category("Paths")]
    [DisplayName("Path to files folder")]
    public string FilesPath { get; set; }

    /// <summary>
    /// Gets or sets a value that determines if the site is locked for Angel logins.
    /// </summary>
    [DisplayName("Angel locked")]
    [Description("Determines if the site is locked for Angel logins.")]
    public bool AngelLocked { get; set; }

    /// <summary>
    /// Gets or sets information about the SQL Server database being used.
    /// </summary>
    [DisplayName("Database")]
    [ReadOnly(true)]
    public SqlServerSettings SqlSettings { get; set; }

    /// <summary>
    /// Gets the path to the Dynamicweb standard application. Returns RootPath for a standard solution, or the parent of AdminPath for a custom soltuion.
    /// </summary>
    /// <returns></returns>
    public string GetDynamicwebAppPath()
    {
      if (IsCustom)
      {
        var folder = new DirectoryInfo(AdminPath);
        if (folder.Parent == null)
        {
         throw new Exception("Couldn't get DirectoryInfo for path " + AdminPath); 
        }
        return folder.Parent.FullName;
      }
      return RootPath;
    }

    /// <summary>
    /// Provides information about SQL Server related data for the database used by the site.
    /// </summary>
    [TypeConverter(typeof(DatabaseOptionsConverter))]
    public class SqlServerSettings
    {
      /// <summary>
      /// The SQL Server instance this site connects to.
      /// </summary>
      [DisplayName("SQL Server Instance")]
      [Description("The SQL Server instance this site connects to.")]
      public string Instance { get; set; }

      /// <summary>
      /// The full version of SQL Server.
      /// </summary>
      [DisplayName("SQL Server version")]
      [Description("The full version of SQL Server.")]
      public string Version { get; set; }

      /// <summary>
      /// The database version number used by this instance of SQL Server.
      /// </summary>
      [DisplayName("Database version")]
      [Description("The database version number used by this instance of SQL Server.")]
      public int DatabaseVersion { get; set; }

      /// <summary>
      /// The name of the SQL Server database being used.
      /// </summary>
      [DisplayName("Database name")]
      [Description("The name of the SQL Server database being used.")]
      public string Database { get; set; }

      /// <summary>
      /// Whether the site uses integrated security to connect to the database.
      /// </summary>
      [DisplayName("Integrated Security")]
      [Description("Whether the site uses integrated security to connect to the database.")]
      public bool IntegratedSecurity { get; set; }


      /// <summary>
      /// Builds a SQL Connecting string from the current server and database info.
      /// </summary>
      public string BuildConnectionString()
      {
        var builder = new SqlConnectionStringBuilder() { DataSource = Instance, InitialCatalog = Database, IntegratedSecurity = IntegratedSecurity };
        return builder.ToString();
      }
    }
  }
}
